package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.FormColumn;
import com.ruoyi.system.service.IFormColumnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.FormDefineMapper;
import com.ruoyi.system.domain.FormDefine;
import com.ruoyi.system.service.IFormDefineService;

import javax.annotation.Resource;

/**
 * 单定义Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-16
 */
@Service
public class FormDefineServiceImpl extends ServiceImpl<FormDefineMapper,FormDefine> implements IFormDefineService
{
    @Autowired
    private FormDefineMapper formDefineMapper;
    @Resource
    private IFormColumnService formColumnService;

    @Override
    public boolean saveFormDesc(FormDefine formDefine) {
        this.save(formDefine);
       JSONObject desc = formDefine.getFormDesc();
       if(desc==null){
           return true;
       }
       JSONObject formConfig = desc.getJSONObject("formConfig");
        formConfig.getString("");

        JSONArray widgetList = desc.getJSONArray("widgetList");
        List<FormColumn> formColumnList =new ArrayList<>();
        widgetList.forEach(it->{
          JSONObject ito =  new JSONObject((HashMap)it);
          this.getAllFormItem(ito,formColumnList);
        });
        formColumnList.forEach(it->{
            it.setDefineId(formDefine.getId());
        });
        formColumnService.saveBatch(formColumnList);
        return true;
    }
     @Override
    public boolean updateFormDesc(FormDefine formDefine) {
        this.updateById(formDefine);
        JSONObject desc = formDefine.getFormDesc();
         if(desc==null){
             return true;
         }
        JSONObject formConfig = desc.getJSONObject("formConfig");
        formConfig.getString("");

        JSONArray widgetList = desc.getJSONArray("widgetList");
        List<FormColumn> formColumnListNew =new ArrayList<>();
        widgetList.forEach(it->{
            JSONObject ito =  new JSONObject((HashMap)it);
            this.getAllFormItem(ito,formColumnListNew);
        });
        formColumnListNew.forEach(it->{
            it.setDefineId(formDefine.getId());
        });

        LambdaQueryWrapper<FormColumn> columnLambdaQueryWrapper = new LambdaQueryWrapper<>();
        columnLambdaQueryWrapper.eq(FormColumn::getDefineId,formDefine.getId());
        List<FormColumn> formColumnListOld = formColumnService.list(columnLambdaQueryWrapper);
        formColumnListOld.forEach(it->{
            FormColumn findNew =   formColumnListNew.stream().filter(ix->ix.getComponentId().equals(it.getComponentId())).findFirst().orElse(null);
            if(findNew ==null){
                formColumnService.removeById(it.getId());
            }
        });
        List<FormColumn>  formColumnListOld2 = formColumnService.list(columnLambdaQueryWrapper);
        formColumnListNew.forEach(it->{
            FormColumn  oldFined = formColumnListOld2.stream().filter(ix->ix.getComponentId().equals(it.getComponentId())).findFirst().orElse(null);
            if(oldFined!=null){
                it.setId(oldFined.getId());
            }
        });
        formColumnListNew.forEach(it->{
            it.setDefineId(formDefine.getId());
        });




        formColumnService.saveOrUpdateBatch(formColumnListNew);
        return true;
    }






    private void getAllFormItem(JSONObject ito,List<FormColumn> formColumnList ){
        Boolean formItemFlag = ito.getBoolean("formItemFlag");
        if((formItemFlag!=null)&&formItemFlag){
            String componentId = ito.getString("id");
            String componentType = ito.getString("type");
            JSONObject option =ito.getJSONObject("options");
            String label =option.getString("label");
            FormColumn column =new FormColumn();
            column.setComponentId(componentId);
            column.setComponentType(componentType);
            column.setLabel(label);
            formColumnList.add(column);
        }
        JSONArray widgetList  = ito.getJSONArray("widgetList");
        JSONArray tabs  = ito.getJSONArray("tabs");
        JSONArray rows  = ito.getJSONArray("rows");
        JSONArray cols  = ito.getJSONArray("cols");
        if(widgetList!=null){
            widgetList.forEach(it->{
                JSONObject itoChild  =  new JSONObject((HashMap)it);
                this.getAllFormItem(itoChild,formColumnList);
            });
        }
        if(tabs!=null){
            tabs.forEach(it->{
                JSONObject itoChild  =  new JSONObject((HashMap)it);
                this.getAllFormItem(itoChild,formColumnList);
            });
        }

        if(rows!=null){
            rows.forEach(it->{
                JSONObject itoChild  =  new JSONObject((HashMap)it);
                this.getAllFormItem(itoChild,formColumnList);
            });
        }

        if(cols!=null){
            cols.forEach(it->{
                JSONObject itoChild  =  new JSONObject((HashMap)it);
                this.getAllFormItem(itoChild,formColumnList);
            });
        }
    }


    /**
     * 查询单定义
     *
     * @param id 单定义主键
     * @return 单定义
     */
    @Override
    public FormDefine selectFormDefineById(Long id)
    {
        return formDefineMapper.selectFormDefineById(id);
    }

    /**
     * 查询单定义列表
     *
     * @param formDefine 单定义
     * @return 单定义
     */
    @Override
    public List<FormDefine> selectFormDefineList(FormDefine formDefine)
    {
        return formDefineMapper.selectFormDefineList(formDefine);
    }

    /**
     * 新增单定义
     *
     * @param formDefine 单定义
     * @return 结果
     */
    @Override
    public int insertFormDefine(FormDefine formDefine)
    {
        formDefine.setCreateTime(DateUtils.getNowDate());
        return formDefineMapper.insertFormDefine(formDefine);
    }

    /**
     * 修改单定义
     *
     * @param formDefine 单定义
     * @return 结果
     */
    @Override
    public int updateFormDefine(FormDefine formDefine)
    {
        formDefine.setUpdateTime(DateUtils.getNowDate());
        return formDefineMapper.updateFormDefine(formDefine);
    }

    /**
     * 批量删除单定义
     *
     * @param ids 需要删除的单定义主键
     * @return 结果
     */
    @Override
    public int deleteFormDefineByIds(Long[] ids)
    {
        return formDefineMapper.deleteFormDefineByIds(ids);
    }

    /**
     * 删除单定义信息
     *
     * @param id 单定义主键
     * @return 结果
     */
    @Override
    public int deleteFormDefineById(Long id)
    {
        return formDefineMapper.deleteFormDefineById(id);
    }
}
